package repositories

import (
	"github.com/kataras/iris/v12"
	"huanteng/web/global/application"
	//"gorm.io/gorm"
	"huanteng/vendors/paginate"
	"huanteng/web/models"
)

/*type AdminRepository interface {
	FindByName(name string) (user models.Admin)
	Create(user *models.Admin)
	AdminList(ctx iris.Context,user *[]models.Admin)
	Update(id uint, user *models.Admin)
	Delete(id uint)

}*/

type AdminRepository struct {
}

/*func NewRepository (db *gorm.DB) AdminRepository{
	return &AdminRepository{
		db:db,
	}
}*/

func (a *AdminRepository)Delete(id []uint)  {
	application.DB.Delete(&models.Admin{},id)
	//application.DB.Where("id = ?", id).Delete(&models.Admin{})
}
func (a *AdminRepository)Update(user *models.Admin)  {
	where := models.Admin{ID:user.ID}
	application.DB.Where(where).Updates(&user)
}


func (a *AdminRepository)AdminList(ctx iris.Context,user *[]models.Admin)  {
	//application.DB.SetupJoinTable(&user,"roles",&models.AdminRole{})
	where:=models.Admin{}
	where.Name=ctx.URLParam("username")
	 status:=uint(ctx.URLParamInt64Default("status",1))
	 where.Status=&status
	application.DB.Where(where).Preload("Role").Scopes(paginate.Paginate(&user))

}
func (a *AdminRepository)FindByName(name string) (user models.Admin)  {
	 application.DB.Where("name=?",name).First(&user)
	 return
}
func (a *AdminRepository)FindById(id uint) (user models.Admin)  {
	application.DB.Where("id=?",id).First(&user)
	return
}

func (a *AdminRepository)FindRoleById(id uint) []uint{
	var ids []uint
	application.DB.Model(models.AdminRole{}).Where(models.AdminRole{AdminID:id}).Pluck("role_id" ,&ids)
	return ids
}

func (a *AdminRepository)Create(user *models.Admin) {
	application.DB.Create(user)

}
